<template>
  <div class="editMember-container">
    <el-row class="editMember-main">
      <el-col :span="24"
              class="editMember-title">
        编辑构件
      </el-col>
      <el-col :span="24"
              class="editMember-form">
        <el-form :model="ruleForm"
                 status-icon
                 :rules="rules"
                 ref="ruleForm"
                 label-width="80px"
                 class="demo-ruleForm">
          <el-form-item label="构件类型"
                        prop="member_type">
            <el-select v-model="ruleForm.member_type"
                       class="normal-input"
                       placeholder="选择构件类型">
              <el-option v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="构件编号"
                        prop="member_num">
            <el-input class="normal-input"
                      v-model="ruleForm.member_num"></el-input>
          </el-form-item>
          <el-form-item label="楼号"
                        prop="floor">
            <el-input class="normal-input"
                      v-model="ruleForm.floor"></el-input>
          </el-form-item>
          <el-form-item label="层号"
                        prop="layer">
            <el-input class="normal-input"
                      v-model="ruleForm.layer"></el-input>
          </el-form-item>
          <el-form-item label="加工厂"
                        prop="plant">
            <el-select v-model="ruleForm.plant"
                       class="normal-input"
                       placeholder="选择加工厂">
              <el-option v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="详细参数"
                        prop="params">
            <el-upload class="avatar-uploader"
                       action="https://jsonplaceholder.typicode.com/posts/"
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess"
                       :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl"
                   :src="imageUrl"
                   class="avatar">
              <i v-else
                 class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
            <el-button type="success"
                       @click="submit('ruleForm')">保存</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data () {
    return {
      options: [],
      imageUrl: '',
      ruleForm: {
        member_num: "",
        member_type: "",
        floor: "",
        layer: "",
        plant: "",
        params: ""
      },
      rules: {
        member_type: [
          { required: true, message: '请选择构件类型', trigger: 'change' },
        ],
        member_num: [
          { required: true, message: '请输入构件编号', trigger: 'blur' },
        ],
        floor: [
          { required: true, message: '请输入楼号', trigger: 'blur' },
        ],
        layer: [
          { required: true, message: '请输入层号', trigger: 'blur' },
        ],
        plant: [
          { required: true, message: '请选择加工厂', trigger: 'change' },
        ],
      }
    }
  },
  methods: {
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //保存功能
    submit (ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          return false;
        }
      });
    },
    //取消功能
    resetForm (ruleForm) {
      this.$refs[ruleForm].resetFields();
    }
  },
  created () {

  },
  mounted () {

  }
}
</script>

<style scoped lang='scss'>
.editMember-container {
  .editMember-main {
    background: #fff;
    padding: 20px;
    .editMember-title {
      font-size: 18px;
      color: #1890ff;
      margin-bottom: 20px;
    }
    .editMember-form {
      .normal-input {
        width: 40%;
      }
    }
  }
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
